#include "mcmf.hpp"
void MCMF::clear(){
    // tot = 1;
    // top = 1;
    // ans = 0;
    // for(int i=1;i<=n;i++)
    //     head[i] = 0;
    //G.clear(n);
    ncnt = 1;
    for(int i=1;i<=n;i++)
        head[i] = mark[i] = 0;
}

MCMF::MCMF(int siteNum, int clientNum){
    //debug(siteNum);
    //debug(clientNum);
    this->siteNum = siteNum;
    this->clientNum = clientNum;
    this->SOURCE_POINT = this->siteNum + this->clientNum + 1;
    this->n = this->TARGET_POINT = this->SOURCE_POINT + 1;
    clear();
}

void MCMF::addEdge(int _siteID, int _clientID, int _cap, ll _cost){
    // this->add(_siteID, this->siteNum + _clientID,  _cost, _cap);
    _addEdge(_siteID, this->siteNum + _clientID, _cap,   _cost);
    // G.addEdge(,0);
}

void MCMF::addEdgeFromSource(int _siteID, int _cap, ll _cost){
    // this->add(this->SOURCE_POINT, _siteID, _cost, _cap);
    _addEdge(SOURCE_POINT, _siteID, _cap, _cost);
}

void MCMF::addEdgeToTarget(int _clientID, int _cap, ll _cost){
    _addEdge(_clientID + siteNum, TARGET_POINT, _cap, _cost);
    // this->add(this->siteNum + _clientID, this->TARGET_POINT,  _cost,_cap);
}

void MCMF::mcmf(int &_flow, ll &_cost){
    // auto p = G.minCostFlow(SOURCE_POINT, TARGET_POINT, n);
    // long long a = 0;
    _flow = simplex(SOURCE_POINT, TARGET_POINT, &_cost);
    // _cost = a;
    // _flow = p.first;
    // _cost = p.second;

}

void MCMF::getSponsor(int clientID, std::vector<std::pair<int,int>>& receptor){
    //std::cerr << clientID << " ," << h[this->siteNum + clientID] << ", " << this->siteNum + clientID<< "\n";
    // for(int i=G.adj[this->siteNum + clientID];i;i=e[i].nxt){
    //     if(e[i].fl > 0 && e[i].to <= siteNum){
    //         receptor.push_back(std::make_pair(e[i].to, e[i].fl));
    //     }
    // }
    for(int i = head[siteNum + clientID];i;i=edge[i].nxt){
        if(edge[i].cap > 0 && edge[i].to <= siteNum){
            receptor.push_back(std::make_pair(edge[i].to, edge[i].cap));
        }
    }
}